#coding:utf-8
__author__ = 'TANUKI'
import time


def iOSVerificationCode(driver):
	try:
		driver.execute_script("mobile:dragFromToForDuration",
							  {"duration": 0.5, "element": None, "fromX": 118, "fromY": 446, "toX": 290,
							   "toY": 446})
		time.sleep(5)
		driver.execute_script("mobile:dragFromToForDuration",
							  {"duration": 0.5, "element": None, "fromX": 118, "fromY": 446, "toX": 275,
							   "toY": 446})
		time.sleep(5)
		driver.execute_script("mobile:dragFromToForDuration",
							  {"duration": 0.5, "element": None, "fromX": 118, "fromY": 446, "toX": 265,
							   "toY": 446})
	except:
		pass

#获得机器屏幕大小x,y
def getSize(driver):
	x = driver.get_window_size()['width']

	y = driver.get_window_size()['height']

	return (x, y)


#屏幕向上滑动
def shanghua(driver, t):
	l = getSize(driver)

	x1 = int(l[0] * 0.5)  #x坐标

	y1 = int(l[1] * 0.75)   #起始y坐标

	y2 = int(l[1] * 0.25)   #终点y坐标

	driver.swipe(x1, y1, x1, y2,t)


#屏幕向下滑动
def xiahua(driver, t):
	l = getSize(driver)

	x1 = int(l[0] * 0.5)  #x坐标

	y1 = int(l[1] * 0.25)   #起始y坐标

	y2 = int(l[1] * 0.75)   #终点y坐标

	driver.swipe(x1, y1, x1, y2,t)


#屏幕向左滑动
def zuohua(driver, t):
	l = getSize(driver)

	x1 = int(l[0]*0.75)

	y1 = int(l[1]*0.5)

	x2 = int(l[0]*0.05)

	driver.swipe(x1,y1,x2,y1,t)

#屏幕向右滑动
def youhua(driver, t):
	l = getSize(driver)

	x1 = int(l[0]*0.05)

	y1 = int(l[1]*0.5)

	x2 = int(l[0]*0.75)

	driver.swipe(x1,y1,x2,y1,t)

def custom(driver, _x1, _y1, _x2, _y2, move, t):
	"""
	:param driver: driver
	:param _x1: x1坐标
	:param _y1: y1坐标
	:param _x2: x2坐标
	:param _y2: y2坐标
	:param move: 方向
	:param t: 滑动距离
	:return:  无
	"""
	l = getSize(driver)

	x1 = int(l[0] * _x1)

	y1 = int(l[1] * _y1)

	x2 = int(l[0] * _x2)

	y2 = int(l[1] * _y2)

	if move == 'shang':
		print ("执行上滑")
		driver.swipe(x1, y1, x1, y2,t)

	elif move == 'xia':
		driver.swipe(x1, y1, x1, y2,t)

	elif move == 'zuo':
		driver.swipe(x1,y1,x2,y1,t)

	elif move == 'you':
		driver.swipe(x1,y1,x2,y1,t)


def huadongVerificationCode(driver):
	from appium.webdriver.common.touch_action import TouchAction
	actions = TouchAction(driver)
	try:
		actions.long_press(x=315, y=1417, duration=500).move_to(x=746, y=1417).release().perform()
		time.sleep(5)
		# driver.tap(x = 746, y = 1417)
		print("验证码滑动完成执行一次11111111111111111")

		time.sleep(5)
		print("等待5秒完成")
		actions.long_press(x=315, y=1417, duration=500).move_to(x=770, y=1417).release().perform()
		print("验证码滑动完成执行2次22222222222222222")
		time.sleep(5)
		actions.long_press(x=315, y=1417, duration=1000).move_to(x=710, y=1417).release().perform()
	except:
		pass

def logout(driver, label = "label == \"Beta Test\""):
	driver.find_element_by_ios_predicate("%s"%label).click()
	time.sleep(5)
	driver.find_element_by_ios_predicate("label == \"切换测试环境\" AND name == \"切换测试环境\" AND value == \"切换测试环境\"").click()


def comingsoon(driver):
	driver.find_element_by_ios_predicate("label == \"alert close icon\"").click()
	# label == "alert close icon"


def iOSshanghua(driver):
	driver.execute_script("mobile:dragFromToForDuration",
						  {"duration": 0.5, "element": None, "fromX": 210, "fromY": 600, "toX": 210,
						   "toY": 150})

def xiafangzuohua(driver):
	driver.execute_script("mobile:dragFromToForDuration",
						  {"duration": 0.5, "element": None, "fromX": 300, "fromY": 600, "toX": 100,
						   "toY": 600})

def switchDealorWallet(driver, name):
	driver.find_element_by_ios_predicate("label == \"%s\""%name).click()